package com.elock.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Repository;

import com.elock.base.core.dao.impl.DaoCenter;
import com.elock.base.core.util.ConvertUtil;
import com.elock.base.core.util.DateUtil;
import com.elock.base.core.util.WebUtils;

/**
 * @author 仁德
 */
@Repository("terminalPositionDao")
public class TerminalPositionDao extends DaoCenter {

	public Integer count(String terminalNum, String startTime, String endTime) {

		List<Object> list = new ArrayList<Object>();
		Long CompanyInfoId = WebUtils.getCurrentCompanyInfoId();
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT count(1) FROM terminal_position WHERE  1=1 ");
		if (CompanyInfoId != null) {
			sql.append(" and company_info_id=" + CompanyInfoId);
		}
		if (!ConvertUtil.isEmpty(terminalNum)) {
			sql.append(" and terminal_num= ? ");
			list.add(terminalNum);
		}
		if (!ConvertUtil.isEmpty(startTime)) {
			sql.append(" and create_date >= ?");
			list.add(DateUtil.convert(startTime));
		}
		if (!ConvertUtil.isEmpty(endTime)) {
			sql.append(" and create_date < ?");
			list.add(DateUtils.addDays(DateUtil.convert(endTime), 1));
		}

		Object[] objs = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			objs[i] = list.get(i);
		}

		Integer count = getNativeDao().findInt(sql.toString(), objs);

		return count;

	}

	public List<Map<String, Object>> ExportProductPriceHead(String terminalNum, String startTime, String endTime,
			Integer page, Integer size) {

		List<Object> list = new ArrayList<Object>();
		Long CompanyInfoId = WebUtils.getCurrentCompanyInfoId();
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT * FROM terminal_position WHERE  1=1 ");
		if (CompanyInfoId != null) {
			sql.append(" and company_info_id=" + CompanyInfoId);
		}
		if (!ConvertUtil.isEmpty(terminalNum)) {
			sql.append(" and terminal_num= ? ");
			list.add(terminalNum);
		}
		if (!ConvertUtil.isEmpty(startTime)) {
			sql.append(" and create_date >= ?");
			list.add(DateUtil.convert(startTime));
		}
		if (!ConvertUtil.isEmpty(endTime)) {
			sql.append(" and create_date < ?");
			list.add(DateUtils.addDays(DateUtil.convert(endTime), 1));
		}
		sql.append(" order by create_date desc ");

		if (page != null && size != null) {
			sql.append(" limit " + (size * (page - 1)) + "," + size);
		}

		Object[] objs = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			objs[i] = list.get(i);
		}

		List<Map<String, Object>> maps = getNativeDao().findListMap(sql.toString(), objs, 0);

		return maps;

	}
}